package com.xuexiang.xui_lib.popup;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import com.xuexiang.xui_lib.ResourceTable;

public class GoodComponent extends PopupDialog implements IGoodComponent {
    private Context mContext;
//    private PixelMapElement mBackground;
    private Color mTextColor = DEFAULT_TEXT_COLOR;
    //private int mDistance = DEFAULT_DISTANCE;
    private int mToY = DEFAULT_TO_Y_DELTA;
    //private int mFromY = DEFAULT_FROM_Y_DELTA;
    private int mDuration = DEFAULT_DURATION;
    private float mFromAlpha = DEFAULT_FROM_ALPHA;
    private float mToAlpha = DEFAULT_TO_ALPHA;
    private String mText = DEFAULT_TEXT;
    private int mTextSize = DEFAULT_TEXT_SIZE;
    private AnimatorGroup mAnimatorGroup;

    private Text mTextContent;
    private Image mImage;

    /**
     * GoodComponent
     *
     * @param context context
     * @param component component
     */
    public GoodComponent(Context context, Component component) {
        super(context, component);
        mContext = context;
        setMode(LayoutAlignment.TOP);
        setCustomComponent(initComponent(context));
    }

    private Component initComponent(Context context) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_xui_pop, null, false);
        mTextContent = (Text) component.findComponentById(ResourceTable.Id_tv);
        mImage = (Image) component.findComponentById(ResourceTable.Id_image);
        mImage.setVisibility(Component.HIDE);
        return component;
    }

    @Override
    public void show() {
        super.show();
    }

    private AnimatorGroup createAnimator(Component target) {
        AnimatorGroup animatorGroup = new AnimatorGroup();
        AnimatorProperty animator1 = new AnimatorProperty();
        animator1.setTarget(target);
        animator1.moveToY(mToY);
        animator1.alphaFrom(mFromAlpha);
        animator1.alpha(mToAlpha);
        animator1.setDuration(mDuration);

        animatorGroup.build().addAnimators(animator1);
        animatorGroup.setStateChangedListener(new Animator.StateChangedListener() {
            /**
             * onStart
             *
             * @param animator animator
             */
            @Override
            public void onStart(Animator animator) {
            }

            /**
             * onStop
             *
             * @param animator animator
             */
            @Override
            public void onStop(Animator animator) {
            }

            /**
             * onCancel
             *
             * @param animator animator
             */
            @Override
            public void onCancel(Animator animator) {
            }

            /**
             * onEnd
             *
             * @param animator animator
             */
            @Override
            public void onEnd(Animator animator) {
                hide();
            }

            /**
             * onPause
             *
             * @param animator animator
             */
            @Override
            public void onPause(Animator animator) {
            }

            /**
             * onResume
             *
             * @param animator animator
             */
            @Override
            public void onResume(Animator animator) {
            }
        });
        return animatorGroup;
    }

    /**
     * onShowing
     */
    @Override
    protected void onShowing() {
        super.onShowing();
        mAnimatorGroup.start();
    }

    /**
     * Color.getIntColor("#CCCCCC");
     *
     * @param color color
     * @return return
     */
    @Override
    public IGoodComponent setTextColor(int color) {
        mTextColor = new Color(color);
        mTextContent.setTextColor(mTextColor);
        return this;
    }

    @Override
    public IGoodComponent setTextSize(int textSize) {
        mTextSize = AttrHelper.fp2px(textSize, mContext);
        mTextContent.setTextSize(mTextSize);
        return this;
    }

    @Override
    public IGoodComponent setTextInfo(String text, int textColor, int textSize) {
        setTextX(text);
        setTextColor(textColor);
        setTextSize(textSize);
        return this;
    }

    @Override
    public IGoodComponent setBackgroundRes(int mediaId) {
        super.setBackColor(Color.TRANSPARENT);
        mTextContent.setVisibility(Component.HIDE);
        mImage.setVisibility(Component.VISIBLE);
        mImage.setPixelMap(mediaId);
        mAnimatorGroup = createAnimator(mImage);
        return this;
    }

    @Override
    public IGoodComponent setDistance(int distance) {
        //this.mDistance = distance;
        return this;
    }

    @Override
    public IGoodComponent setTranslateY(int fromY, int toY) {
        //mFromY = fromY;
        mToY = toY;
        return this;
    }

    @Override
    public IGoodComponent setAlpha(float fromAlpha, float toAlpha) {
        mFromAlpha = fromAlpha;
        mToAlpha = toAlpha;
        return this;
    }

    @Override
    public IGoodComponent setDurationX(int duration) {
        mDuration = duration;
        return this;
    }

    @Override
    public void reset() {
        mText = DEFAULT_TEXT;
        //mDistance = DEFAULT_DISTANCE;
        mDuration = DEFAULT_DURATION;
        mFromAlpha = DEFAULT_FROM_ALPHA;
        //mFromY = DEFAULT_FROM_Y_DELTA;
        mToAlpha = DEFAULT_TO_ALPHA;
        mToY = DEFAULT_TO_Y_DELTA;
        mAnimatorGroup = createAnimator(mTextContent);
    }

    @Override
    public IGoodComponent setTextX(String text) {
        mText = text;
        mImage.setVisibility(Component.HIDE);
        mTextContent.setText(mText);
        mAnimatorGroup = createAnimator(mTextContent);
        return this;
    }
}
